﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lk.Api.JwtTokenAuth;
using Lk.Common.Cache;
using Lk.Common.Helper;
using Lk.IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Lk.Api.Controllers
{

    [AllowAnonymous]
    public class LoginController : BaseApiController
    {
        private readonly IUserService _userService;
        private readonly ICacheService _cacheService;
        public LoginController(IUserService userService, ICacheService cacheService)
        {
            _userService = userService;
            _cacheService = cacheService;
        }

        [HttpGet("token")]
        [MapToApiVersion("1")]
        public ActionResult<string> GetJwtStr(string userName, string pwd, long id = 1, string sub = "Client", int expiresSliding = 30, int expiresAbsoulute = 30)
        {
            var tokenModel = new TokenModelJwt
            {
                Uid = id,
                Role = sub
            };
            string jwtStr = JwtHelper.IssueJwt(tokenModel);

          
            return jwtStr;
        }

        [HttpGet("test")]
        public async Task<string> Test()
        {
            _cacheService.Add("test2", "11111111111111");

            var result = await _userService.QueryByID("78A24AAA-DB22-434F-A7F9-CB1F84ED54A0");

            try
            {
                // int s = int.Parse(v);
            }
            catch (Exception)
            {
                // return new StatusCodeResult(429);
            }
            return JsonHelper.ToJson(result);
        }
        [HttpGet("test2")]
        public async Task<string> Test2()
        {


            var result = await _userService.GetList();
            return JsonHelper.ToJson(result);
        }
    }
}